package com.sloth.bear.controller;

import com.sloth.bear.annotation.NoAuth;
import com.sloth.bear.dto.request.*;
import com.sloth.bear.dto.vo.DocDetailsVo;
import com.sloth.bear.dto.vo.DocListVo;
import com.sloth.bear.dto.vo.DocumentItemVo;
import com.sloth.bear.service.DocItemService;
import com.sloth.bear.service.DocService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档管理
 */
@RestController
@RequestMapping("/doc")
@RequiredArgsConstructor
public class DocController {

    private final DocService docService;
    private final DocItemService docItemService;

    /**
     * 创建文档
     */
    @PostMapping
    public Long createDoc(@Valid @RequestBody DocRequest request) {
        return docService.createDoc(request);
    }

    /**
     * 文档列表
     */
    @GetMapping("/list")
    @NoAuth
    public List<DocListVo> listDoc(@Valid DocListRequest request) {
        return docService.listDoc(request);
    }

    /**
     * 文档详情
     */
    @GetMapping("/{id}")
    @NoAuth
    public DocDetailsVo detailsDoc(@PathVariable Long id) {
        return docService.detailsDoc(id);
    }

    /**
     * 更新文档
     */
    @PutMapping("/{id}")
    public void updateDoc(@PathVariable Long id, @Valid @RequestBody DocUpdateRequest request) {
        docService.updateDoc(id, request);
    }

    /**
     * 拖拽文档
     */
    @PutMapping("/{id}/drag")
    public void dropDoc(@PathVariable Long id, @RequestBody DragRequest request) {
        docService.dropDoc(id, request);
    }

    /**
     * 删除文档
     */
    @DeleteMapping("/{id}")
    public void deleteDoc(@PathVariable Long id) {
        docService.deleteDoc(id);
    }

    /**
     * 创建文档子项
     */
    @PostMapping("/item")
    public DocumentItemVo createItem(@Valid @RequestBody DocItemRequest request) {
        return docItemService.createItem(request);
    }

    /**
     * 根据ID获取子项详情
     */
    @GetMapping("/item/{id}")
    public DocumentItemVo getItemById(@PathVariable Long id) {
        return docItemService.getItemById(id);
    }

    /**
     * 更新文档子项
     */
    @PutMapping("/item/{id}")
    public void updateItem(@PathVariable Long id, @Valid @RequestBody DocItemRequest request) {
        docItemService.updateItem(id, request);
    }

    /**
     * 切换文档子项完成状态
     */
    @PutMapping("/item/{id}/toggle-complete")
    public DocumentItemVo toggleComplete(@PathVariable Long id) {
        return docItemService.toggleComplete(id);
    }

    /**
     * 删除文档子项
     */
    @DeleteMapping("/item/{id}")
    public boolean deleteItem(@PathVariable Long id) {
        return docItemService.deleteItem(id);
    }
}